﻿using System.Web.Mvc;
using AutoMapper;
using Guan.CMS.CommandProcessor.Dispatcher;
using Guan.CMS.Domain.Commands.Contact;
using Guan.CMS.Web.ViewModels.Contact;

namespace Guan.CMS.Web.Controllers
{
    public class ContactUsController : Controller
    {
        private readonly ICommandBus _commandBus;
        public ContactUsController(ICommandBus commandBus)
        {
            this._commandBus = commandBus;
        }

        public ActionResult Index(bool success=false)
        {
            if (success)
            {
                ViewBag.SubmitSuccess = true;
            }
            return View();
        }

        [HttpPost,ActionName("Index")]
        public ActionResult Save(ContactFormModel form)
        {
            if (ModelState.IsValid)
            {
                form.IP = Request.ServerVariables.Get("Remote_Addr");
                var command = Mapper.Map<ContactFormModel, AddContactCommand>(form);
                var result = _commandBus.Submit(command);
                if (result.Success)
                {
                    return RedirectToAction("Index", new {success = true});
                }
            }
            return View("Index",form);
        }
    }
}